package com.lmb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmb.bean.Dish;
import com.lmb.bean.Setmeal;
import com.lmb.dao.SetmealDao;
import com.lmb.dao.SetmealDishDao;
import com.lmb.dto.SetmealDto;
import com.lmb.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.xml.ws.soap.Addressing;
import java.time.LocalDateTime;

/**
 * @projectName: reggie
 * @package: com.lmb.service.impl
 * @className: SetmealServicelmpl
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/18 16:48
 */
@Service
public class SetmealServicelmpl extends ServiceImpl<SetmealDao,Setmeal> implements SetmealService{

    @Autowired
    private SetmealDishDao setmealDishDao;

    @Override
    public void upStatuss(Long[] ids, Integer status, HttpSession session) {
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishLambdaQueryWrapper.eq(id!= null,Setmeal::getId,id);
            Setmeal setmeal = new Setmeal();

            if (status==0){
                setmeal.setStatus(0);
            }

            if (status==1){
                setmeal.setStatus(1);
            }

            setmeal.setUpdateUser((Long) session.getAttribute("employee"));
            Long employee = (Long) session.getAttribute("employee");
            System.out.println("修改人"+employee);
            System.out.println(setmeal.getUpdateUser());
            setmeal.setUpdateTime(LocalDateTime.now());
            System.out.println(setmeal);
            update(setmeal, dishLambdaQueryWrapper);
        }
    }

    @Override
    public void deleteDishs(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal= getById(id);
            if (!setmeal.getStatus().equals(0)){
                throw new ClassCastException("此菜品还在起售状态");
            }
            this.removeById(id);
        }
    }

    @Override
    public boolean addSetmealAndSetmealDish(SetmealDto setmealDto, HttpSession session) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmealDto.getName() != null,Setmeal::getName,setmealDto.getName());
        int count = count(setmealLambdaQueryWrapper);
        if (setmealDto.getName() == null ||count>0){
            throw new ClassCastException("菜品名重复");
        }

        Long employeeId = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(employeeId) ;
        setmealDto.setCreateTime(LocalDateTime.now());

        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setUpdateUser(employeeId);

        boolean save = save(setmealDto);

        setmealDto.getSetmealDishes().stream().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(employeeId);
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(employeeId);
            setmealDishDao.insert(setmealDish);
        });
        return save?true:false;

    }
}
